#!/bin/sh
#
#

MWEMU="/sbin/memu"

Unpack()
{
	echo -n "Extracting ${1} ... "
	rm -rf ${BUILD_PATH}/${1}
	if [ -f ${SRC_PATH}/${1}.tar.bz2 ]; then
		tar jxf ${SRC_PATH}/${1}.tar.bz2 -C ${BUILD_PATH} || exit 1
	elif [ -f ${SRC_PATH}/${1}.tar.gz ]; then
		tar zxf ${SRC_PATH}/${1}.tar.gz -C ${BUILD_PATH} || exit 1
	else
		echo "No ${$1} source package found!"
		exit 1
	fi
	echo "OK."
}


cd ${SYSROOT_PATH}

for dir in lib usr/lib bin usr/bin sbin usr/sbin sys dev etc/init.d proc root home var tmp; do
	mkdir -vp ${SYSROOT_PATH}/${dir} || exit 1
done

if [ ! -c dev/console ]; then
	mknod -m 666 dev/console c 5 1
fi

if [ ! -c dev/null ]; then
	mknod -m 666 dev/null c 1 3
fi

echo "MaxWit Linux ${MWL_VER}" > ${SYSROOT_PATH}/etc/issue

#cp -a ${SYSROOT_PATH}/usr/${TARGET_PLAT}/lib/${LIBS} usr/lib/

RCS="${SYSROOT_PATH}/etc/init.d/rcS"
rm -f ${RCS}
cp -v ${PREGEN_PATH}/rcS ${RCS}
chmod 755 ${RCS}

if [ ${USE_UDEV} = "1" ]; then
	sed -i '/^mdev/d' ${SYSROOT_PATH}/etc/init.d/rcS

	PKG_LIST="${MWP_ZLIB} ${MWP_UDEV}"
else
	echo -n "Configure mdev ... "
	sed -i '/^udev/d' ${SYSROOT_PATH}/etc/init.d/rcS
	sed -i 's/udev/mdev/g' ${SYSROOT_PATH}/etc/init.d/rcS
	cp ${PREGEN_PATH}/mdev.conf ${SYSROOT_PATH}/etc/
	cp -a ${PREGEN_PATH}/mdev ${SYSROOT_PATH}/lib/
	echo "OK."

	PKG_LIST="${MWP_ZLIB}"
fi


PKG_LIST="${PKG_LIST} \
	${MWP_BUSYBOX} \
	${MWP_LIBPNG} \
	${MWP_JPEG} \
	${MWP_TIFF} \
	${MWP_LIBUNGIF} \
	${MWP_FREETYPE} \
	${MWP_NCURSES} \
	${MWP_ALSA_LIB} \
	${MWP_ALSA_UTILS} \
	${MWP_TSLIB} \
	${MWP_DFB} \
	${MWP_SDL} \
	${MWP_SDL_IMAGE} \
	${MWP_SDL_TTF} \
	${MWP_SDL_MIXER} \
	${MWP_LIBMAD} \
	${MWP_LIBID3TAG} \
	${MWP_LIBUSB} \
	${MWP_DFB_EX} \
	${MWP_LIBUSB} \
	${MWP_MADPLAY} \
	${MWP_FBV} \
	${MWP_MPG123} \
	${MWP_MPLAYER} \
	${MWP_PRBOOM}"

# to add:
#	${MWP_USB_UTILS} \
#	${MWP_GPM} \
#	${MWP_MESA_LIB} \
#	${MWP_MESA_GLUT} \
#	${MWP_MESA_DEMO} \

for pkg in ${PKG_LIST}; do
	echo
	echo "\t******  ${pkg}  ******"
	echo

	Unpack ${pkg} || exit 1
	if [ ${pkg} = "jpegsrc.v6b" ]; then
		pkg="jpeg-6b"
	fi
	cd ${BUILD_PATH}/${pkg} || exit 1
	pkg_build=${pkg%-*}
	pkg_build=`echo ${pkg_build} | tr A-Z a-z`
	${MWL_TOP}/linux/${pkg_build}.sh || exit 1
done


echo
# fixme
KERN_OPT="ARCH=arm CROSS_COMPILE=${TARGET_PLAT}- INSTALL_MOD_PATH=${SYSROOT_PATH}"

# fixme 
QEMU_CONSOLE=0
if [ x"${QEMU_CONSOLE}" = x"1" ]; then
	EXTRA_QEMU_OPT="-serial stdio"
	EXTRA_COMMAND_LINE="console=ttyAMA0" # fixme
fi

BuildKernel()
{
	PWD=`pwd`
	cd ${BUILD_PATH}/${MWP_KERNEL}

	make ${KERN_OPT} ${1}_defconfig
	sed -i 's/# CONFIG_AEABI.*/CONFIG_AEABI=y/' .config
	# fixme
	sed -i '/CONFIG_OABI_COMPAT/d' .config
	sed -i '/CONFIG_AEABI/a\# CONFIG_OABI_COMPAT is not set/' .config
	sed -i 's/\(CONFIG_SYSFS_DEPRECATED.*\)=y/# \1 is not set/' .config

	make ${KERN_OPT} -j4 && \
	make ${KERN_OPT} modules_install ||  exit 1

	cp -v arch/arm/boot/zImage ${IMAGES_PATH}/zImage.${1}
	cd ${PWD}

	# fixme
	case $1 in
	realview | mainstone | spitz)
	qemu-system-arm -M $1 -kernel ${IMAGES_PATH}/zImage.${1} -net nic -net tap ${EXTRA_QEMU_OPT} -append "root=/dev/nfs rw nfsroot=${QEMU_HOST_IP}:/maxwit/sysroot ip=${QEMU_TARGET_IP}:${QEMU_HOST_IP}:${QEMU_HOST_IP}:255.255.255.0:maxwit.googlecode.com:eth0:off ${EXTRA_COMMAND_LINE}"

	echo qemu-system-arm -M $1 -kernel ${IMAGES_PATH}/zImage.${1} -net nic -net tap \${EXTRA_QEMU_OPT} -append \"root=/dev/nfs rw nfsroot=${QEMU_HOST_IP}:/maxwit/sysroot ip=${QEMU_TARGET_IP}:${QEMU_HOST_IP}:${QEMU_HOST_IP}:255.255.255.0:maxwit.googlecode.com:eth0:off \${EXTRA_COMMAND_LINE}\" >> ${MWEMU}
	chmod a+x ${MWEMU}
	;;

	*)
	echo "qemu do NOT support $1, skipping"
	;;
	esac
}

cat > ${MWEMU} << EOF
#!/bin/sh

QEMU_CONSOLE=0
if [ x"\${QEMU_CONSOLE}" = x"1" ]; then
	EXTRA_QEMU_OPT="-serial stdio"
	EXTRA_COMMAND_LINE="console=ttyAMA0" # fixme
fi
EOF

Unpack ${MWP_KERNEL}

# BuildKernel mainstone 
# BuildKernel spitz
BuildKernel realview
# BuildKernel at91sam9261ek
# BuildKernel at91sam9263ek

